<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd">新增</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon style="fontSize:12px" icon="ant-design:down-outlined"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
    </BasicTable>
    <AccessKeyShowModal @register="registerShowModal" @close="reload" />
    <AccessKeyModal @register="registerModal"  @success="reload"/>
    <VerifyModal @register="registerVerifyModal" :submitCallback="handleSubmitVerify" biz-type="access_key"/>
  </div>
</template>
<script lang="ts" name="access-key" setup>
import {ref} from 'vue';
import {BasicTable, TableAction} from '/@/components/Table';
import {useGlobSetting} from '/@/hooks/setting';
import {columns, searchFormSchema} from './accessKey.data';
import {
  batchDeleteAccessKey,
  deleteAccessKey,
  getAccessKeyDetailsById,
  listAccessKey,
  saveOrUpdate
} from './accessKey.api';
import {useListPage} from '/@/hooks/system/useListPage';
import {useMessage} from '/@/hooks/web/useMessage';
import {useModal} from '/@/components/Modal';
import AccessKeyModal from './AccessKeyModal.vue';
import AccessKeyShowModal from './AccessKeyShowModal.vue';
import {VerifyModal} from '/@/components/BizVerify';
import {Icon} from '/@/components/Icon';

const [registerModal, { openModal }] = useModal();
const [registerShowModal, { openModal:  openShowModal}] = useModal();
const [registerVerifyModal, { openModal: openVerifyModal }] = useModal();
const { createMessage } = useMessage();
const glob = useGlobSetting();
const currentRecord = ref<any>({})

// 列表页面公共参数、方法
const { prefixCls, tableContext } = useListPage({
  designScope: 'access-secret',
  tableProps: {
    title: 'API访问密钥',
    api: listAccessKey,
    columns: columns,
    formConfig: {
      schemas: searchFormSchema,
      fieldMapToTime: [['fieldTime', ['beginDate', 'endDate'], 'YYYY-MM-DD HH:mm:ss']],
    },
    actionColumn: {
      width: 150,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
  },
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

/**
 * 新增事件
 */
async function handleAdd() {
  openModal(true, {
    isUpdate: false,
  });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteAccessKey({ id: record.id }, () => {
    reload()
    createMessage.success("删除成功")
  });
}

/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDeleteAccessKey({ ids: selectedRowKeys.value }, () => {
    reload()
    createMessage.success("删除成功")
  });
}

/**
 * 查看
 */
const handleEdit = async (record) => {
  openModal(true, {
    record: {
      id: record.id,
      name: record.name
    },
    isUpdate: true,
  });
}

/**
 * 前置处理
 */
const handleShowSecretKeyPre = async (record) => {
  currentRecord.value = record
  if (!record.isNeedVerify) {
    const ret = await getAccessKeyDetailsById({id: currentRecord.value.id})
    openShowModal(true, {
      ...ret
    })
    return
  }
  openVerifyModal(true, {});
}

const handleSubmitVerify = async (data) => {
  console.log('submitEmailVerify: ', data);
  const ret = await getAccessKeyDetailsById({...data, id: currentRecord.value.id})
  openShowModal(true, {
    ...ret
  })
  console.log('accessKeyDetails: ', ret);
}

/**
 * 更新状态
 */
 const handleUpdateStatus = async (record) => {
  await saveOrUpdate({id: record.id, status: record.status === '0' ? '1' : '0'}, true)
  reload()
  createMessage.success("更新成功")
}


/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
    {
      label: '删除',
      color: 'error',
      ifShow: record.sendStatus != 1,
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
    
    {
      label: record.status === '1' ? '禁用' : '启用',
      onClick: handleUpdateStatus.bind(null, record),
    }
  ]
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    },
    {
      label: '密钥KEY',
      onClick: handleShowSecretKeyPre.bind(null, record),
    }
  ]
}

</script>


<style>
.secret {
  margin-bottom: 5px;
}
</style>
